package com.tb.common;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * comment
 *
 * @author zhao yuan
 * @since 2018/5/19
 */
public class CollectionTest {

    /**
     * 生成指定集合的排列组合
     */
    @Test
    public void permutationTest(){
        List<Integer> integers = Arrays.asList(1, 2, 3);
        Collection<List<Integer>> permutations = Collections2.permutations(integers);
        System.out.println(JSON.toJSONString(permutations));
    }

    /**
     * 指定元素个数
     */
    @Test
    public void frequencyTest(){
        List<Integer> integers = Arrays.asList(1, 2, 3,1,1,1,2,null,null);
        int frequency = Collections.frequency(integers, 1);
        System.out.println(frequency);  // 4
        int nullFrequency = Collections.frequency(integers, null);
        System.out.println(nullFrequency); // 2
    }

    /**
     * 指定元素个数
     */
    @Test
    public void filterTest(){
        List<Integer> integers = Arrays.asList(1, 2, 3,5,1,6,2,null,null);
        Collection<Integer> result = Collections2.filter(integers, new Predicate<Integer>() {
            @Override
            public boolean apply(Integer input) {
                return input == null || input > 2;
            }
        });
        System.out.println(JSON.toJSONString(result)); // [3,5,6,null,null]
    }
}
